package com.frxuexi.shop.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.base.db.OrderEntity
import com.frxuexi.base.mvi.BaseActivity
import com.frxuexi.base.mvi.RequestType
import com.frxuexi.base.mvi.UiState
import com.frxuexi.lib_base.alipay.MyPay
import com.frxuexi.shop.R
import com.frxuexi.shop.adapter.OrderAdapter
import com.frxuexi.shop.databinding.ActivityOrderBinding
import com.frxuexi.shop.intent.OrderIntent
import com.frxuexi.shop.viewmodel.OrderViewModel
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@Route(path = "/shop/OrderActivity")
@AndroidEntryPoint
class OrderActivity : BaseActivity<ActivityOrderBinding,OrderIntent,OrderViewModel>() {

    val orderAdapter by lazy { OrderAdapter() }
    lateinit var orderEntity:OrderEntity
    override fun getVM(): OrderViewModel {
        return viewModels<OrderViewModel>().value
    }

    override fun getBind(): ActivityOrderBinding {
        return ActivityOrderBinding.inflate(layoutInflater)
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.DEFAULT->{
                                val entities = it.data as Collection<OrderEntity>
                                orderAdapter.apply {
                                    data.clear()
                                    addData(entities)
                                    notifyDataSetChanged()
                                }
                            }
                            RequestType.UPDATE->{
                                orderAdapter.remove(orderEntity)
                                orderAdapter.notifyDataSetChanged()
                            }

                            else -> {}
                        }
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        EventBus.getDefault().register(this)
        binding.tabOrder.apply {
            addTab(newTab().setText("待付款"),true)
            addTab(newTab().setText("待发货"),false)
            addTab(newTab().setText("待收货"),false)
            addTab(newTab().setText("待评论"),false)
            addTab(newTab().setText("退货"),false)
            addOnTabSelectedListener(object :OnTabSelectedListener{
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    vm.sendIntent(OrderIntent.getOrdersByStatus(tab?.position!!+1))
                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {
                }

                override fun onTabReselected(tab: TabLayout.Tab?) {
                }
            })
        }
        binding.orderRv.apply {
            adapter=orderAdapter
            layoutManager=LinearLayoutManager(this@OrderActivity)
        }

        vm.sendIntent(OrderIntent.getOrdersByStatus(1))

        orderAdapter.addChildClickViewIds(R.id.btn_order,R.id.btn_no)
        orderAdapter.setOnItemChildClickListener { adapter, view, position ->
            orderEntity = orderAdapter.data[position]
            when(view.id){
                R.id.btn_no->{
                    when(orderEntity.status){
                        1->{
                            orderEntity.status=6
                            vm.sendIntent(OrderIntent.updateOrder(orderEntity))
                        }
                        else->{
                            orderEntity.status=5
                            vm.sendIntent(OrderIntent.updateOrder(orderEntity))
                        }
                    }
                }
                R.id.btn_order->{
                    when(orderEntity.status){
                        1->{
                            MyPay.payV2(orderEntity.sum.toString(),this@OrderActivity)
                        }
                        2->{
                            orderEntity.status=3
                            vm.sendIntent(OrderIntent.updateOrder(orderEntity))
                        }
                        3->{
                            orderEntity.status=4
                            vm.sendIntent(OrderIntent.updateOrder(orderEntity))
                        }
                    }
                }
            }

        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun getPayStatus(status:String){
        if(status=="payOk"){
            orderEntity.status=2
            vm.sendIntent(OrderIntent.updateOrder(orderEntity))
        }else{
            ToastUtils.showLong("支付失败")
        }
    }
}